
import sys
import time
import json
from datetime import datetime
from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import urllib.parse
from mongoengine import *
from selenium.webdriver.common.action_chains import ActionChains


class needdeleteuid(Document):
    account = StringField()
    uid = StringField()
    deleted = IntField()
    addTime = DateTimeField()
    updatetime = DateTimeField()
    meta = {'strict': False}


def NewDriver():
    chrome_options = Options()
    chrome_options.add_argument("user-data-dir=" + path2 + 'userinfo' + account)
    driver = webdriver.Chrome(
        path, options=chrome_options)
    driver.maximize_window()
    return driver

def userList():
    print(account,accountUid)
    deleteUserList = needdeleteuid.objects(deleted=0,account=account)[:100]
    for item in deleteUserList:
        # print(item.uid)
        deleteUser(item)




def deleteUser(item):
    print('needdelete',item.uid)
    userUrl = 'https://weibo.com/u/{UID}?refer_flag=1005050005_&is_all=1'
    urlWithUid = userUrl.format(UID=item.uid)
    if urlWithUid == 'https://weibo.com/u/?refer_flag=1005050005_&is_all=1':
        print(1+a)
    driver.get(urlWithUid)
    time.sleep(3)
    try:
        moreMenu = driver.find_element_by_class_name('W_btn_pf_menu')
        # moreMenu.click()
        action = ActionChains(driver)
        action.move_to_element_with_offset(moreMenu, 0, 100).perform()
        action.move_to_element(moreMenu).perform()
        time.sleep(1)
        # tlinks = driver.find_elements_by_class_name('tlink')
        # tlinks[2].click()
        deleteFans = driver.find_elements_by_link_text('移除粉丝')
        blacklist = driver.find_elements_by_link_text('加入黑名单')
        if len(deleteFans) != 0:
            deleteFans[0].click()
            time.sleep(1)
            # driver.find_element_by_link_text('确定').click()
            confirm = driver.find_element_by_link_text('确定')
            confirm.click()
            time.sleep(1)
            # deleteButton = driver.find_element_by_class_name('W_btn_pf_menu')
            # deleteButton.click()
            newdata = needdeleteuid(
                id = item.id,
                account = item.account,
                uid = item.uid,
                deleted = 1,
                addTime = item.addTime,
                updatetime = datetime.now()
            )
            newdata.save()
            time.sleep(0.5)
        elif len(deleteFans) == 0 and len(blacklist) != 0:
            newdata = needdeleteuid(
                id = item.id,
                account = item.account,
                uid = item.uid,
                deleted = -1,
                addTime = item.addTime,
                updatetime = datetime.now()
            )
            newdata.save()
    except:
        pass



if __name__ == '__main__':
    account = sys.argv[1]
    accountUid = sys.argv[2]
    mode = int(sys.argv[3])
    path = 'd:/work/weiboAdmin/python/chromedriver.exe'
    path2 = 'd:/work/weiboAdmin/profile/'
    if mode == 1:
        path = 'd:/weiboAdmin/python/chromedriver.exe'
        path2 = 'd:/weiboAdmin/profile/'
        #connect('weibo', host='192.168.1.176', port=27017)
        connect('weibo', host='localhost', port=27017)
    
    else:
        connect('weibo', host='localhost', port=27017)
    # print(path,mode,'good')
    driver = NewDriver()
    driver.get("https://weibo.com")
    time.sleep(3)
    
    # try:
    #     userList()
    # except Exception as e:
    #     print(str(e),'error')


    while True:
        try:
            try:
                userList()
                # sendUserToUserInfo()
            except:
                pass
        except:
            continue